package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishesService;
import com.sky.service.impl.DishesServiceImpl;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api
public class DishesController {

    @Autowired
    private DishesServiceImpl dishesServiceimpl;

    @Autowired
    private DishesService dishesService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result dishesInsert(@RequestBody DishDTO dishDTO){

        log.info("新增菜品：{}", dishDTO);

        dishesServiceimpl.dishesInsert(dishDTO);

        //清理缓存数据
        String key = "dish_" + dishDTO.getCategoryId();
        redisDelete(key);

        return Result.success();
    }

    /**
     * 修改菜品
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result dishUpdate(@RequestBody DishDTO dishDTO){
        log.info("修改菜品:{}", dishDTO);

        dishesServiceimpl.dishUpdate(dishDTO);

        redisDelete("dish_*");
        return Result.success();
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result dishDelete(@RequestParam List<Long> ids){

        log.info("删除菜品:{}", ids);
        dishesServiceimpl.dishDelete(ids);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        redisDelete("dish_*");
        return Result.success();
    }

    /**
     * 分页查询
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public Result<DishVO> queryById(@PathVariable Long id){
        log.info("根据id查询:{}", id);
        DishVO dishVO = dishesServiceimpl.queryById(id);
        log.info("dishVO数据：{}", dishVO);
        return Result.success(dishVO);
    }

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("page")
    @ApiOperation("分页查询")
    public Result<PageResult> queryList(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询:{}", dishPageQueryDTO);
        PageResult pageResult = dishesServiceimpl.queryList(dishPageQueryDTO);
        Result<PageResult> success = Result.success(pageResult);
        return success;
    }

    @GetMapping("/list")
    @ApiOperation("根据id查菜品")
    public Result<List<Dish>> ById(Long categoryId){

        log.info("根据id查菜品:{}", categoryId);
        List<Dish> dishList = dishesService.listById(categoryId);
        return Result.success(dishList);
    }

    public void redisDelete(String id){
        Set keys = redisTemplate.keys(id);
        redisTemplate.delete(keys);
    }
}
